
package com.distrimas.senta.servicios.conexion;

import com.distrimas.senta.servicios.comun.*;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.SQLException;
import java.util.Map;
import org.apache.commons.dbcp.BasicDataSource;
//import org.apache.tomcat.dbcp.dbcp.BasicDataSource;
public class Conexion {

    private static BasicDataSource bds;
    private static Connection con;
    private static Map<String, String> conexionProperties;
    private static final String path = "conexiones.properties";

    static {
        String urlDb = null;
        String usuarioDb = null;
        String passwordDb = null;
        String driver = null;
        int initialSize = 0;
        int maxActive = 0;
        int maxIdle = 0;
        int maxWait = 0;
        String validationQuery = null;
        try {
            Propiedades prop = new Propiedades();
            conexionProperties = prop.leerPropiedades(path);
            urlDb = conexionProperties.get("Url");
            usuarioDb = conexionProperties.get("Username");
            passwordDb = conexionProperties.get("Password");
            driver = conexionProperties.get("Driver");
            initialSize = Integer.parseInt(conexionProperties.get("InitialSize"));
            maxActive = Integer.parseInt(conexionProperties.get("MaxActive"));
            maxIdle = Integer.parseInt(conexionProperties.get("MaxIdle"));
            maxWait = Integer.parseInt(conexionProperties.get("MaxWait"));
            validationQuery = conexionProperties.get("ValidationQuery");
        } catch (Exception e) {
            e.printStackTrace();
        }

        try {
            Class.forName(driver);
            bds = new BasicDataSource();
            bds.setDriverClassName(driver);
            bds.setUrl(urlDb);
            bds.setUsername(usuarioDb);
            bds.setPassword(passwordDb);
            bds.setInitialSize(initialSize);
            bds.setMaxActive(maxActive);
            bds.setMaxIdle(maxIdle);
            bds.setMaxWait(maxWait);
            bds.setValidationQuery(validationQuery);
        } catch (ClassNotFoundException e) {
            e.printStackTrace();
        }
    }

    private synchronized static void setCon(Connection con) {
        Conexion.con = con;
    }

    private synchronized static void ConexionDB() {
        try {
            setCon(DriverManager.getConnection(conexionProperties.get("Url"), conexionProperties.get("Username"), conexionProperties.get("Password")));
        } catch (SQLException eSQL) {
            eSQL.printStackTrace();
        } catch (Exception eOtra) {
            eOtra.printStackTrace();
        }
    }

    public synchronized static Connection getConnection() {
        try {
            ConexionDB();
        } catch (Exception e) {
            e.printStackTrace();
        }
        return con;
    }

    public synchronized static void returnConnection(Connection conn) {
        if (conn == null) {
            return;
        }
        try {
            conn.close();
        } catch (SQLException e) {
            e.printStackTrace();
        }
    }
}
